{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "7903ae1e",
   "metadata": {},
   "source": [
    "# 变量作用域\n",
    "\n",
    "函数外部定义的变量可以在函数里面使用、修改和重新赋值，这涉及到Python中变量作用域的问题。\n",
    "\n",
    "变量作用域（Variable Scope）是指变量的有效生存空间。\n",
    "\n",
    "在函数中，Python会按照以下顺序，在各个作用域的命名空间中寻找变量：\n",
    "- 函数局部作用域（Local Function Scope）。\n",
    "- 闭包作用域（Enclosing Scope）。\n",
    "- 全局作用域（Global Scope）。\n",
    "- 内置作用域（Built-in Scope）。\n",
    "\n",
    "如果有重名的变量，局部优先于闭包，闭包优先于全局，全局优先于内置。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76d1efc9",
   "metadata": {},
   "source": [
    "## 局部作用域"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5faebca",
   "metadata": {},
   "source": [
    "例如，所有变量在局部作用域的例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "de032f2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def foo(a, b):\n",
    "    c = 1\n",
    "    return a + b + c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c99dddc7",
   "metadata": {},
   "source": [
    "## 全局作用域"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5447fc50",
   "metadata": {},
   "source": [
    "c在全局作用域："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5e0c540b",
   "metadata": {},
   "outputs": [],
   "source": [
    "c = 1\n",
    "def foo(a, b):\n",
    "    return a + b + c\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "4236be5a",
   "metadata": {},
   "source": [
    "## 内置作用域\n",
    "\n",
    "内置函数都在内置作用域，它们都可以在标准库模块builtins中找到："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b6b02099",
   "metadata": {},
   "outputs": [],
   "source": [
    "import builtins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8bc323f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len is builtins.len"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "b47ea655",
   "metadata": {},
   "source": [
    "## 闭包作用域\n",
    "\n",
    "嵌套函数中，a相对于inner()函数来说，是一个属于闭包作用域的变量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "98ff8d8e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=1\n"
     ]
    }
   ],
   "source": [
    "def outer():\n",
    "    a = 1\n",
    "    def inner():\n",
    "        print(f\"{a=}\")\n",
    "    inner()\n",
    "    \n",
    "outer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f6692d3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
